package com.artpri.dao.impl;

import java.util.List;

import com.artpri.dao.OrderInfoDao;
import com.artpri.model.OrderInfo;
import com.artpri.service.OrderInfoService.OrderType;

/**
 * 
 * @author ZhangSi
 *
 */
public class OrderInfoDaoImpl extends HibernateDaoImpl<OrderInfo> implements OrderInfoDao{

	@Override
	public List<OrderInfo> getOrderInfoByUserNum(Integer userNum) {
		return find("from OrderInfo o where o.userNum=?0", userNum);
	}

	@Override
	public List<OrderInfo> getOrderList(Integer userNum, OrderType type) {
		return getOrderList(userNum, type, 0, 0, false);
	}

	@Override
	public List<OrderInfo> getOrderListByPage(Integer userNum, OrderType type, int pageNo, int pageSize) {
		return getOrderList(userNum, type, pageNo, pageSize, true);
	}
	
	private List<OrderInfo> getOrderList(Integer userNum, OrderType type, int pageNo, int pageSize, boolean findByPage) {
		StringBuffer sbf = new StringBuffer("from OrderInfo o where o.userNum=?0");
		
		String orderBy = " order by orderCreatTime";
		
		Object[] params = null;
		
		switch (type) {
		case ALL:
			sbf.append(orderBy);
			params = new Object[] {userNum};
			break;
		case DONE_DEAL:
			sbf.append(" and o.orderState=?1").append(orderBy);
			params = new Object[] {userNum, 5};
			break;
		case WAIT_PAY:
			sbf.append(" and o.orderState=?1 and o.orderPayState=?2").append(orderBy);
			params = new Object[] {userNum, 0, 0};
			break;
		case WAIT_GOODS:
			sbf.append(" and o.orderState=?1").append(orderBy);
			params = new Object[] {userNum, 3};
			break;
		case WAIT_COMMENT:
			sbf.append(" and o.orderState=?1").append(orderBy);
			params = new Object[] {userNum, 4};
			break;
		case CANCELED:
			sbf.append(" and o.orderState=?1").append(orderBy);
			params = new Object[] {userNum, 2};
			break;
		case UNFINISH:
			sbf.append(" and o.orderState!=2 and o.orderState!=5").append(orderBy);
			params = new Object[] {userNum};
		default:
			break;
		}
		
		if (findByPage) {
			return findByPage(sbf.toString(), pageNo, pageSize, params);
		} else {
			return find(sbf.toString(), params);
		}
	}
	
	@Override
	public Integer getOrderCount(Integer userNum, OrderType type) {
		String hql = "select count(*) from OrderInfo o where o.userNum=?0 ";
		switch (type) {
		case ALL:
			return findCount(hql, userNum);
		case WAIT_PAY:
			return findCount(hql + " and o.orderState=?1 and o.orderPayState=?2", userNum, 0, 0);
		case WAIT_COMMENT:
			return findCount(hql + " and o.orderState=?1", userNum, 4);
		case WAIT_GOODS:
			return findCount(hql + " and o.orderState=?1", userNum, 3);
		case CANCELED:
			return findCount(hql + " and o.orderState=?1", userNum, 2);
		case DONE_DEAL:
			return findCount(hql + " and o.orderState=?1", userNum, 5);
		default:
			break;
		}
		return null;
	}

	
}
























/*switch (type) {
case ALL:
	list = find(hql, userNum);
	break;
case DONE_DEAL:
	list = find(hql + " and o.orderState=?1" + orderBy, userNum, 5);
	break;
case WAIT_PAY:
	list = find(hql + " and o.orderState=?1 and o.orderPayState=?2" + orderBy, userNum, 1, 2);
	break;
case WAIT_GOODS:
	list = find(hql + " and o.orderState=?1" + orderBy, userNum, 3);
	break;
case WAIT_COMMENT:
	list = find(hql + " and o.orderState=?1" + orderBy, userNum, 4);
	break;
case CANCELED:
	list = find(hql + " and o.orderState=?1" + orderBy, userNum, 2);
	break;
case UNFINISH:
	list = find(hql + " and o.orderState!=2 and o.orderState!=5" + orderBy, userNum);
default:
	break;
}*/
